<?php
/*
	收入明细
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'isfentan'=>intval($_RQ['isfentan']));
	$return['params']['isfentan']=$_RQ['isfentan'];
	if($_RQ['type']=='islast'){
		$params['lastbufee >']=0;
		$return['params']['type']=$_RQ['type'];
	}
	if($_RQ['storeid']){
		$params['fromstoreid']=intval($_RQ['storeid']);
		$return['params']['storeid']=$_RQ['storeid'];
	}
	if(!empty($_RQ['title'])){
		$params['title like']='%'.$_RQ['title'].'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['mobile'])){
		$params['mobile']=$_RQ['mobile'];
		$return['params']['mobile']=$_RQ['mobile'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = $_RQ['psize']?:50;
	if($_RQ['psize']){
		$return['params']['psize']=$_RQ['psize'];
	}
	$total='';
	$list=pdo_getslice('jy_yxw_yonghu',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['member']=$this->model('yonghu')->member_show($item['id']);
			$infee_log=pdo_get('jy_yxw_infee_log',array('type'=>1,'memberid'=>$item['memberid'],'uniacid'=>$_SESSION['uniacid']));
			$item['paytime']=$infee_log['up_time'];
			$item['payfee']=$infee_log['fee'];
			$item['pay_type']=$infee_log['pay_type'];
			$item['open_time']=pdo_getcolumn('jy_yxw_class',array('id'=>$item['classid'],'uniacid'=>$_SESSION['uniacid']),'open_time');
			$item['zhanghu']='公户';
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'export'=>'export',
		'list'=>array('data'=>'list','tip'=>'[<red>温馨提示</red>]：这里处理各门店的收费情况','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'member','title'=>'用户信息','type'=>'member'),
			array('name'=>'fromstoreid','title'=>'所属门店','type'=>'select2','options'=>'stores'),
			array('name'=>'cardno','title'=>'身证号','type'=>'string'),
			array('name'=>'mobile','title'=>'手机号','type'=>'string'),
			array('name'=>'classid','title'=>'售后群','type'=>'select2','options'=>'class'),
			array('type'=>'radio2','title'=>'状态','name'=>'status','options'=>'status'),
			array('name'=>'open_time','title'=>'开班时间','type'=>'time'),
			array('name'=>'payfee','title'=>'单费','type'=>'string'),
			array('name'=>'paytime','title'=>'收款日期','type'=>'time'),
			array('name'=>'pay_type','title'=>'交费方式','type'=>'radio2','options'=>'pay_types'),
			array('name'=>'zhanghu','title'=>'到账账户','type'=>'string'),
		),'action'=>array(
			array('type'=>'href','action'=>'jy_yxw/juese14.fentan_xs/check/','title'=>'前往分摊','show'=>array('isfentan'=>0)),
		)),
		'search'=>array(
			array('type'=>'string','width'=>'15','title'=>'手机号','name'=>'mobile','help'=>''),
			array('type'=>'string','width'=>'15','title'=>'售后名称','name'=>'title','help'=>''),
			array('name'=>'storeid','width'=>15,'title'=>'所属门店','type'=>'select','options'=>'stores'),
		)
	);
	$return['stores']=$this->getall('store',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['class']=$this->getall('class',array('storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');	
	$return['status']=$this->yonghu_status;
	$return['htmls']=$htmls;
	$return['pay_types']=$this->pay_types;
	exi($return);
}
if($_RQ['op']=='check'){
	if($_RQ['postdata']){
		$post=get_postdata();
		if($post['fee1'] + $post['fee2'] + $post['fee3']>$post['payfee']){
			exi('【补贴费】+【学校】+【销售门店】必须等于【单费】!','error');
		}
		$id=$post['id'];
		$sn=$post['isnum']+1;
		$data=array(
			'fee1'=>$post['fee1'],
			'fee2'=>$post['fee2'],
			'fee3'=>$post['fee3'],
			'zhanghu'=>$post['zhanghu'],
			'remark'=>trim($post['remark']),
			'status'=>intval($post['status']),
		);
		$data['uniacid']=$_SESSION['uniacid'];
		$data['yonghuid']=$id;
		$data['memberid']=$post['memberid'];
		$data['storeid']=$post['storeid'];
		$data['up_time']=time();
		$data['createtime']=time();
		$data['up_workerid']=$_SESSION['workerid'];
		$this->insert('fentan_log',$data);
		if($post['status']==1){
			pdo_update('jy_yxw_yonghu',array('isfentan'=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		}
		exi('操作成功！','','/juese14.fentan_xs//isfentan=0');
	}
	if($_RQ['id']){
		$item=pdo_get('jy_yxw_yonghu',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
		$item['storetitle']=pdo_getcolumn('jy_yxw_store',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['storeid']),'title');
		$item['classtitle']=pdo_getcolumn('jy_yxw_class',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['classid']),'title');
		$item['classtitle']=$item['classtitle']?:'无';
		$item['statustitle']=$this->yonghu_status[$item['status']];
		$infee_log=pdo_get('jy_yxw_infee_log',array('type'=>1,'memberid'=>$item['memberid'],'uniacid'=>$_SESSION['uniacid']));
		$item['paytime']=$infee_log['up_time'];
		$item['payfee']=$infee_log['fee'];
		$item['pay_type']=$infee_log['pay_type'];
		$item['open_time']=pdo_getcolumn('jy_yxw_class',array('id'=>$item['classid'],'uniacid'=>$_SESSION['uniacid']),'open_time');
		$item['open_time']=$item['open_time']?date('Y-m-d',$item['open_time']):'无';
		$item['paytime']=date('Y-m-d',$item['paytime']);
		$item['zhanghu']='公户';
		$item['pay_typetitle']=$this->pay_types[$item['pay_type']];
	}else{
		exi('请先选择记录','error');
	}
	$return['item']=$item;	
	$htmls=array(
		'type'=>'edit',
	);
	$htmls['edit']=array(
		array('name'=>'id','title'=>'ID','type'=>'copy'),
		array('name'=>'storetitle','title'=>'所属门店','type'=>'copy'),
		array('name'=>'title','title'=>'姓名','type'=>'copy'),
		array('name'=>'cardno','title'=>'身证号','type'=>'copy'),
		array('name'=>'mobile','title'=>'手机号','type'=>'copy'),
		array('name'=>'classtitle','title'=>'售后群','type'=>'copy'),
		array('name'=>'statustitle','title'=>'状态','type'=>'copy'),
		array('name'=>'open_time','title'=>'开班时间','type'=>'copy'),
		array('name'=>'payfee','title'=>'单费','type'=>'copy'),
		array('name'=>'paytime','title'=>'收款日期','type'=>'copy'),
		array('name'=>'pay_typetitle','title'=>'交费方式','type'=>'copy'),
		array('name'=>'zhanghu','title'=>'到账账户','type'=>'copy'),
		array('name'=>'fee1','title'=>'补贴费','type'=>'unitstring','unit'=>'元'),
		array('name'=>'fee2','title'=>'学校','type'=>'unitstring','unit'=>'元'),
		array('name'=>'fee3','title'=>'销售门店','type'=>'unitstring','unit'=>'元'),
		array('type'=>'radio','title'=>'分摊状态','name'=>'status','options'=>'status'),
		array('type'=>'text','title'=>'分摊备注','name'=>'remark'),
	);
	$return['status']=array('待分摊','已分摊','分摊异常');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='export'){
	//售后列表
	$params=array('uniacid'=>$_SESSION['uniacid'],'isfentan'=>intval($_RQ['isfentan']));
	$return['params']['isfentan']=$_RQ['isfentan'];
	if($_RQ['type']=='islast'){
		$params['lastbufee >']=0;
		$return['params']['type']=$_RQ['type'];
	}
	if($_RQ['storeid']){
		$params['fromstoreid']=intval($_RQ['storeid']);
		$return['params']['storeid']=$_RQ['storeid'];
	}
	if(!empty($_RQ['title'])){
		$params['title like']='%'.$_RQ['title'].'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['mobile'])){
		$params['mobile']=$_RQ['mobile'];
		$return['params']['mobile']=$_RQ['mobile'];
	}
	$list=pdo_getall('jy_yxw_yonghu',$params,$fields);
	if($list){
		foreach($list as &$item){
			$item['member']=$this->model('yonghu')->member_show($item['id']);
			$infee_log=pdo_get('jy_yxw_infee_log',array('type'=>1,'memberid'=>$item['memberid'],'uniacid'=>$_SESSION['uniacid']));
			$item['paytime']=$infee_log['up_time'];
			$item['payfee']=$infee_log['fee'];
			$item['pay_type']=$this->pay_types[$infee_log['pay_type']];
			$item['status']=$this->yonghu_status[$item['status']];
			$item['open_time']=pdo_getcolumn('jy_yxw_class',array('id'=>$item['classid'],'uniacid'=>$_SESSION['uniacid']),'open_time');
			$item['open_time']=date('Y-m-d',$item['open_time']);
			$item['paytime']=date('Y-m-d',$item['paytime']);
			$item['zhanghu']='公户';
			$item['fromstore']=pdo_getcolumn('jy_yxw_store',array('id'=>$item['fromstoreid'],'uniacid'=>$_SESSION['uniacid']),'title');
			$item['classid']=pdo_getcolumn('jy_yxw_class',array('id'=>$item['classid'],'uniacid'=>$_SESSION['uniacid']),'title');
		}
	}
	$header=array(
		'fromstore'=>'所属门店',
		'title'=>'售后姓名',
		'cardno'=>'身证号',
		'mobile'=>'手机号',
		'classid'=>'售后群',
		'status'=>'状态',
		'open_time'=>'开班时间',
		'payfee'=>'单费',
		'paytime'=>'收款日期',
		'pay_type'=>'交费方式',
		'zhanghu'=>'到账账户',
	);
	model('core/tools')->export2excel($header,$list,'售后分摊数据');
}
